﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.IO;
using System.Data.SqlClient;
using BookStore.Solution.Configurations;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System.Data.Sql;

namespace BookStore.Solution.Presentation
{
    public partial class frmBackup : DevComponents.DotNetBar.OfficeForm
    {
        
        public frmBackup()
        {
            InitializeComponent();
            this.MaximumSize = this.MaximumSize = this.Size;
        }

        private void frmBackup_Load(object sender, EventArgs e)
        {
        }

        private void btnFolder_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = folderBrowserDialog.SelectedPath;
                DateTime now = DateTime.Now;
                txtFileName.Text = "BookStore" + now.Day + "-" + now.Month + "-" + now.Year + "_" + now.Hour + "-" + now.Minute + "-" + now.Second;
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (txtFolder.Text != "")
            {
                DateTime now = DateTime.Now;
                txtFileName.Text = "BookStore" + now.Day + "-" + now.Month + "-" + now.Year + "_" + now.Hour + "-" + now.Minute + "-" + now.Second;
            }
        }

        private void btnBackUp_Click(object sender, EventArgs e)
        {
            txtFileName.Border.BackColor = txtFolder.Border.BackColor = StyleManager.ColorTint;
            string result = CheckValidated();
            if (result == "")
            {
                BackUp();
            }
            else
                MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private string CheckValidated()
        {
            string result = "";
            if (txtFolder.Text == "")
            {
                result += "Chưa chọn thư mục lưu. ";
                txtFolder.Border.BackColor = Color.Red;
            }
            if (txtFileName.Text == "")
            {
                result += "Chưa nhập tên file. ";
                txtFileName.Border.BackColor = Color.Red;
            }
            string path = txtFolder.Text + @"\" + txtFileName.Text + ".bak";
            if (File.Exists(path))
            {
                result += "Tên file đã tồn tại. ";
                txtFileName.Border.BackColor = Color.Red;
            }
            return result;
        }

        void BackUp()
        {
            btnReset.Enabled = txtFolder.Enabled = txtFileName.Enabled = btnFolder.Enabled = btnBackUp.Enabled = btnCancel.Enabled = false;
            string sConnect = Configs.DbPassport;//chuội kết nối
            string backUpPath = txtFolder.Text + @"\" + txtFileName.Text + ".bak";//đường dẫn file backup
            string dbName = Configs.Database;
            using (SqlConnection cnn = new SqlConnection(sConnect))
            {
                try
                {
                    cnn.Open();
                    dbName = cnn.Database.ToString();

                    ServerConnection sc = new ServerConnection(cnn);
                    Server sv = new Server(sc);

                    // Create backup device item for the backup
                    BackupDeviceItem bdi = new BackupDeviceItem(backUpPath, DeviceType.File);

                    // Create the backup informaton
                    Backup bk = new Backup();
                    bk.PercentComplete += new PercentCompleteEventHandler(bkp_PercentComplete);
                    bk.Devices.Add(bdi);
                    bk.Action = BackupActionType.Database;
                    bk.PercentCompleteNotification = 1;
                    bk.BackupSetDescription = dbName;
                    bk.BackupSetName = dbName;
                    bk.Database = dbName;
                    //bk.ExpirationDate = DateTime.Now.AddDays(30);
                    bk.LogTruncation = BackupTruncateLogType.Truncate;
                    bk.FormatMedia = false;
                    bk.Initialize = true;
                    bk.Checksum = true;
                    bk.ContinueAfterError = true;
                    bk.Incremental = false;
                    // Run the backup
                    bk.SqlBackup(sv);//Exception
                    cnn.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    btnReset.Enabled = txtFolder.Enabled = txtFileName.Enabled = btnFolder.Enabled = btnBackUp.Enabled = btnCancel.Enabled = true;
                }
            }
        }

        private void bkp_PercentComplete(object sender, PercentCompleteEventArgs e)
        {
            this.pbProgress.Value = e.Percent;
            this.pbProgress.Text = e.Percent + "%";
            if (e.Percent == 100)
            {
                MessageBox.Show("BackUp Thành Công", "Thông báo");
                btnCancel.Enabled = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}